package com.one.group.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.query.QueryWrapper;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.*;
import com.one.group.model.entity.MaintenancePlan;
import com.one.group.model.vo.MaintenancePlanVo;
import com.one.group.result.Result;
import com.one.group.service.MaintenancePlanService;
import com.one.group.utils.ResultUtil;
import com.one.group.utils.ThrowUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * {@code @Create:}  2025-09-28  14 : 30
 * {@code @Author:} Cherry
 * {@code @ToUser:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */

@SuppressWarnings({"all"})
@RestController
@RequestMapping("/maintenancePlan")
@Slf4j
@Tag(name = "保养计划接口")
public class MaintenancePlanController {
    @Resource
    private MaintenancePlanService maintenancePlanService;

    @GetMapping("/info")
    @Operation(summary = "保养计划详情接口")
    public Result getInfo(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        QueryWrapper wrapper = new QueryWrapper().eq(MaintenancePlan::getId, id);
        MaintenancePlan maintenancePlan = maintenancePlanService.getOne(wrapper);
        return maintenancePlan != null ? ResultUtil.success(maintenancePlan) : ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
    }


    @PostMapping("/list")
    @Operation(summary = "保养计划列表接口")
    public Result list(@RequestBody MaintenancePlanQueryDto maintenancePlanQueryDto) {

        new PageHelper().startPage(maintenancePlanQueryDto.getPage(), maintenancePlanQueryDto.getLimit());

        List<MaintenancePlanVo> maintenancePlanVoList = maintenancePlanService.listSpMaintenancePlans(maintenancePlanQueryDto);

        PageInfo<MaintenancePlanVo> maintenancePlanVoPageInfo = new PageInfo<>(maintenancePlanVoList);

        return ResultUtil.success(maintenancePlanVoPageInfo);
    }

    @PostMapping("/add")
    @Operation(summary = "保养计划添加接口")
    public Result add(@RequestBody MaintenancePlanAddDto maintenancePlanAddDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(maintenancePlanAddDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        maintenancePlanService.addSpMaintenancePlan(maintenancePlanAddDto);
        return ResultUtil.success("添加成功");
    }

    @PutMapping("/update")
    @Operation(summary = "保养计划修改接口")
    public Result update(@RequestBody MaintenancePlanUpdateDto maintenancePlanUpdateDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(maintenancePlanUpdateDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        maintenancePlanService.updateSpMaintenancePlan(maintenancePlanUpdateDto);
        return ResultUtil.success("修改成功");
    }

    @DeleteMapping("/delete")
    @Operation(summary = "保养计划删除接口")
    public Result delete(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = maintenancePlanService.removeById(id);
        return result ? ResultUtil.success("删除成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @DeleteMapping("/deleteByBatch")
    @Operation(summary = "保养计划批量删除接口")
    public Result delete(@RequestBody BaseDeleteDto baseDeleteDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(baseDeleteDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        maintenancePlanService.batchDeleteSpMaintenancePlans(baseDeleteDto.getIds());
        return ResultUtil.success("删除成功");
    }

    @PostMapping("/execute")
    @Operation(summary = "保养计划执行接口")
    public Result execute(@RequestBody MaintenancePlanExecuteDto executeDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(executeDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        maintenancePlanService.executeSpMaintenancePlan(executeDto);
        return ResultUtil.success("执行成功");
    }
}
